<?php
/**
 * Minhlog Domain_Mapper 文章内容类
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   Domain
 * @package    Domain_DataMapper
 * @copyright  Copyright (c) 2009-2010 Minh.Shu@gmail.com
 * @license    New BSD License
 * @author     Minh.Shu
 * @version    $Id: Article.php 2 2010-01-17 13:49:08Z Minh.Shu $
 *
 */

class Domain_DataMapper_Article extends Domain_DataMapper_Abstract
{
    protected $_tableClassName = 'Domain_Table_Article';

    protected $_dataMapper = array (
            'articleId'      => 'article_id',
            'authorId'       => 'author_id',
            'categoryId'     => 'category_id',
            'articleTitle'   => 'article_title',
            'articleSummary' => 'article_summary',
            'articleContent' => 'article_content',
            'publishTime'    => 'publish_time',
            'clickCount'     => 'click_count',
            'commentCount'   => 'comment_count'
    );

    /**
     * 根据文章ID查询文章数据
     * @param integer $articleId
     * @return object|void
     */
    public function findArticleById($articleId)
    {
        return $this->getTable()->find($articleId)->current();
    }

    /**
     * 查询全部文章数据
     * @param object|null $where
     * @return object|void
     */
    public function findAllArticle($where = null)
    {
        return $this->getTable()->fetchAll($this->findAllArticleStatement($where));
    }

    /**
     * 根据时间查询文章数据
     * @param integer $num
     * @return object|void
     */
    public function findArticleByTime($num = 8)
    {
        return $this->findAll($this->findArticleByConditionStatement('publish_time', $num));
    }

    /**
     * 根据点击数查询文章数据
     * @param integer $num
     * @return object|void
     */
    public function findArticleByClick($num = 8)
    {
        return $this->findAll($this->findArticleByConditionStatement('click_count', $num));
    }

    /**
     * 封装查询全部文章数据SQL对象
     * @param object|null $where
     * @return Zend_Db_Table_Select
     */
    private function findAllArticleStatement($where = null)
    {
        $select = $this->getTable()->select();

        if(null !== $where){
            switch ($where->type) {
                case 'category':
                    $select->where('category_id IN ('.$where->value.')');
                    break;
                case 'tag':
                    $select->where('article_id IN ('.$where->value.')');
                    break;
                case 'archive':
                    $select->where('publish_time >= ?', $where->value['from'])
                            ->where('publish_time < ?', $where->value['to']);
                default :
                    break;
            }
        }
        $select->order('publish_time DESC')
                ->order('article_id DESC');
        return $select;
    }

    /**
     * 封装条件查询文章数据SQL对象
     * @return Zend_Db_Table_Select
     */
    private function findArticleByConditionStatement($where, $num)
    {
        $select = $this->getTable()->select();
        $select->order($where . ' DESC')
                ->limit($num);
        return $select;
    }
}